import 'package:flutter/material.dart';
import 'package:flutter_module/map/ni_map_controller.dart';
import 'package:flutter_module/system/log_util.dart';
import 'package:flutter_module/map/map_manager.dart';
import 'package:flutter_module/util/event_bus.dart';
import 'package:flutter_module/system/constant_config.dart';
import 'package:flutter_module/viewModels/drawer_manager_viewmodel.dart';

import 'map_bottom_drawer.dart';

///地图准星
class MapForesightWidget extends StatefulWidget {
  const MapForesightWidget({Key? key}) : super(key: key);

  @override
  _MapForesightWidgetState createState() => _MapForesightWidgetState();
}

class _MapForesightWidgetState extends State<MapForesightWidget>
    with SingleTickerProviderStateMixin {
  bool _visible = false;
  late AnimationController _animationController;
  late Animation<AlignmentGeometry> _animation;

  @override
  void initState() {
    LogUtil.instance.printf("初始准星");
    super.initState();
    _animationController = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 200));
    _animation = Tween<AlignmentGeometry>(
            begin: const FractionalOffset(0.5, 0.5),
            end: const FractionalOffset(0.5, 0.3))
        .animate(_animationController);
    EventBusUtils.getInstance().on<BottomDrawerStatusChange>().listen((event) {
      if (event.status == DragStatus.stateClose && _visible) {
        setState(() {
          _visible = false;
        });
      } else if (!_visible) {
        _visible = true;
      }
      if ((event.status == DragStatus.stateHalfExpanded ||
              event.status == DragStatus.stateMaxExpanded) &&
          _animationController.value == 0) {
        MapManager.instance.mapController.setMapViewCenter(
            0.0, -0.3 - 48 / ConstantConfig.screenSize.height);
        _animationController.forward();
      } else if ((event.status == DragStatus.stateMinExpanded ||
              event.status == DragStatus.stateClose) &&
          _animationController.value == 1.0) {
        MapManager.instance.mapController.setMapViewCenter(0.0, 0.0);
        _animationController.reverse();
      }
    });
  }

  @override
  void dispose() {
    LogUtil.instance.printf("销毁准星");
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    LogUtil.instance.printf("build 准星 ");
    return AnimatedBuilder(
      animation: _animation,
      builder: (BuildContext context, Widget? child) {
        return AlignTransition(
          alignment: _animation,
          child: Visibility(
            visible: _visible,
            child: child ??
                Image.asset(
                  'assets/icon_map_foresight.png',
                  // key: ConstantConfig.mapForesightId,
                  width: 48,
                  height: 48,
                ),
          ),
        );
      },
      child: Image.asset(
        'assets/icon_map_foresight.png',
        // key: ConstantConfig.mapForesightId,
        width: 48,
        height: 48,
      ),
    );
  }
}
